import java.io.*;

// 写入文件
public class IODemo10 {
    public static void main1(String[] args) {
        try (OutputStream outputStream = new FileOutputStream("D:/Test_IO/output.txt")) {
            outputStream.write('H');// 写入一个字节
            outputStream.write('e');
            outputStream.write('l');
            outputStream.write('l');
            outputStream.write('o');

            outputStream.flush();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main2(String[] args) {
        try (OutputStream outputStream = new FileOutputStream("D:/Test_IO/output.txt")) {
            byte[] b = {'h','e','l','l','o'};
            outputStream.write(b);// 将 b 这个数组里全部字符写入文件

            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main3(String[] args) throws IOException {
        try (OutputStream os = new FileOutputStream("D:/Test_IO/output.txt")) {
            byte[] b = new byte[] {
                    (byte)'G', (byte)'o', (byte)'o', (byte)'d', (byte)'B',
                    (byte)'a', (byte)'d'
            };

            os.write(b, 0, 4);// 从 b 数组的 0 下标开始, 写入 4 个字符
            os.flush();
        }
    }

    public static void main4(String[] args) throws IOException {
        try (OutputStream os = new FileOutputStream("C:/Users/86131/Desktop/Test_IO/output.txt")) {
            String s = "hello";
            byte[] b = s.getBytes();

            os.write(b);
            // 不要忘记 flush
            os.flush();
        }
    }

    public static void main5(String[] args) throws IOException {
        try (OutputStream os = new FileOutputStream("D:/Test_IO/output.txt")) {
            String s = "你好";
            byte[] b = s.getBytes("utf-8");

            os.write(b);// 将 b 这个数组里全部字节写入文件

            os.flush();
        }
    }

    public static void main(String[] args) {
        try (Writer writer = new FileWriter("D:/Test_IO/output.txt")) {
            writer.write('a');
            writer.write('b');
            writer.write('\n');
            writer.write('你');
            writer.write('好');
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
